const url = "https://fly.zwstar.cc/api/"
const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

//登陆获取用户信息
const getUserInfo = function(pr, cb) {
  console.log(pr)
  wx.request({
    url: url + 'wxapp/public/login',
    data: {
      code: pr.code,
      encrypted_data: pr.encrypted_data,
      iv: pr.iv,
      raw_data: pr.raw_data,
      signature: pr.signature
    },
    header: {
      'content-type': 'application/json'
    },
    success: result => {
      cb(result)
    }
  })
}

//性别 生日
const gender = function(openid, gender, birthday, cb) {
  wx.request({
    url: url + '/timecall/timecall/set',
    data: {
      'openid': openid,
      'sex': gender,
      'birthday': birthday
    },
    method: 'POST',
    header: {
      'content-type': 'application/json'
    },
    success: result => {
      cb(result)
    }
  })
}

//添加标签
const addTag = function(name, openid, cb) {
  console.log(openid)
  wx.request({
    url: url + '/timecall/timecall/addTag',
    data: {
      'name': name,
      'openid': openid
    },
    method: 'POST',
    header: {
      'content-type': 'application/json'
    },
    success: result => {
      cb(result)
    },
    fail: res => {
      cb(res)
    }
  })
}

//获取标签
const tag = function(openid, cb) {
  wx.showLoading({
    title: '加载中...',
  })
  wx.request({
    url: url + '/timecall/timecall/tag',
    data: {
      'openid': openid
    },
    method: 'get',
    header: {
      'content-type': 'application/json'
    },
    success: result => {
      cb(result)
      wx.hideLoading()
    }
  })
}

// 上传图片
const upImg = function(cb) {
  wx.chooseImage({
    count: 1, // 默认9
    sizeType: ['compressed'], // 可以指定是原图还是压缩图，默认二者都有
    sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
    success: function(res) {
      // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
      console.log(res)
      let tempFilePaths = res.tempFilePaths[0]
      wx.showLoading({
        title: '上传中...'
      })
      wx.uploadFile({
        url: 'https://fly.zwstar.cc/api/timecall/timecall/uploadImg',
        filePath: tempFilePaths,
        name: 'file',
        success: function(res) {
          cb(res, tempFilePaths)
          wx.showToast({
            title: '上传成功',
            icon: 'success'
          })
        },
        fail: res => {
          console.log(res)
        }
      })
    }
  })
}

const issh = function(cb) {
  wx.request({
    url: url + '/timecall/timecall/issh',
    data: {},
    method: 'get',
    header: {
      'content-type': 'application/json'
    },
    success: result => {
      cb(result)
    }
  })
}

module.exports = {
  formatTime,
  getUserInfo,
  gender,
  addTag,
  tag,
  upImg,
  issh
}